import { createRouter, createWebHashHistory } from 'vue-router'
import store from '../store'

const routes = [
    { path: '/', redirect: '/login' },
    {
        path: '/login',
        component: () =>
            import ('../views/Login.vue'),
        meta: {
            isLogin: false
        }
    },
    {
        path: '/register',
        component: () =>
            import ('../views/Register.vue'),
        meta: {
            isLogin: false
        }
    },
    {
        path: '/main',
        component: () =>
            import ('../views/Main.vue'),
        name: 'main',
        meta: {
            isLogin: true
        },
        children: [{
                path: '',
                redirect: '/own'
            }, {
                // 公司
                path: '/company',
                component: () =>
                    import ('../views/company/Company.vue')
            }, {
                // 项目
                path: '/item',
                component: () =>
                    import ('../views/item/Item.vue')
            }, {
                path: '/itemMember',
                component: () =>
                    import ('../views/item/ItemMember.vue')
            }, {
                // 用户
                path: '/user',
                component: () =>
                    import ('../views/user/User.vue')
            }, {
                // 权限
                path: '/power',
                component: () =>
                    import ('../views/power/Power.vue')
            },
            {
                // 填报
                path: '/report',
                component: () =>
                    import ('../views/report/Report.vue')
            }, {
                // 测试
                path: '/reportPic',
                component: () =>
                    import ('../views/report/ReportPic.vue')
            },
            {
                // 表格
                path: '/reportExcel',
                component: () =>
                    import ('../views/report/ReportExcel.vue')
            },
            {
                path: '/reportRecord',
                component: () =>
                    import ('../views/report/ReportRecord.vue')
            }, {
                // 审核
                path: '/review',
                component: () =>
                    import ('../views/review/Review.vue')
            }, {
                path: '/reviewRecord',
                component: () =>
                    import ('../views/review/ReviewRecord.vue')
            }, {
                // 展示
                path: '/showAll',
                component: () =>
                    import ('../views/show/ShowAll.vue')
            }, {
                path: '/showPart',
                component: () =>
                    import ('../views/show/ShowPart.vue')
            }, {
                // 个人
                path: '/own',
                component: () =>
                    import ('../views/own/Own.vue')
            }
        ],
    },
    {
        path: '/:pathMatch(.*)*',
        name: 'not-found',
        meta: {
            isLogin: false
        },
        component: () =>
            import ('../views/404.vue')
    }
]

const router = createRouter({
    history: createWebHashHistory(),
    routes
})

//挂载路由导航守卫,to表示将要访问的路径，from表示从哪里来，next是下一个要做的操作
// 前置守卫，判断用户是否登录
router.beforeEach((to, from, next) => {
    let path = window.location.href;
    let curHost = path.substring(0, path.indexOf("#"));
    let realHost = window.location.protocol + "//" + window.location.host + "/"
    if (curHost != realHost) { //#号前的地址不正确
        window.location.href = realHost + "#" + to.path //跳转到正确地址
    }

    const token = window.sessionStorage.getItem('satoken');
    const localToken = window.localStorage.getItem('satoken');
    // const autoLogin = window.localStorage.getItem('autoLogin');
    if (to.matched.some(m => m.meta.isLogin)) {
        if (token || localToken) {
            next();
        } else {
            next({ path: '/login' })
        }
    } else {
        if (token || localToken) {
            next({ path: '/main' });
        } else {
            next()
        }
    }
})

export default router